package com.bangbang.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class nearService {
    private static final String GEO_KEY="user_geo";

    @Autowired
    private RedisTemplate redisTemplate;

    //定义上传位置
    public void save(Point point, String userId){
        redisTemplate.opsForGeo().add(GEO_KEY,new RedisGeoCommands.GeoLocation<>(userId,point));
    }

    //附近的人
    public GeoResults<RedisGeoCommands.GeoLocation> near(Point point){
        Distance distance = new Distance(3000,RedisGeoCommands.DistanceUnit.METERS);
        Circle circle = new Circle(point,distance);
        //附近X个人
        RedisGeoCommands.GeoRadiusCommandArgs geoRadiusCommandArgs = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().limit(20);
        GeoResults<RedisGeoCommands.GeoLocation> geoResults = redisTemplate.opsForGeo().radius(GEO_KEY,circle,geoRadiusCommandArgs);
        return geoResults;
    }
}
